#include <iostream>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <dirent.h>
#include <fstream>
#include <string>

#include "include/Monitor.h"
#include "include/MonitorHolder.h"

using namespace std;

int main()
{
    cout << "Starting Blastmate Monitor Simplifier!" << endl;
    MonitorHolder mh;
    DIR *dir;
    struct dirent *ent;
        dir = opendir ("C:\\Users\\Mocuto\\Documents\\Blastware");
    if (dir != NULL) {

    /* print all the files and directories within directory */
    //Skip past the first two dead entries
    readdir (dir);
    readdir (dir);
    while ((ent = readdir (dir)) != NULL) {

        printf ("%s\n", ent->d_name);
        cout << "Reading data from file..." << endl;
        string line;
        string filename = dir->dd_name;
        //Remove the * at the end of the directory
        filename.erase(filename.length()-1, 1);
        filename.append(ent->d_name);
        cout << filename << endl;
        //Open the file into the ifstrema
        ifstream file (filename.c_str());
        if(file.is_open())
        {
            while(file.good())
            {
              getline (file,line);
              if(line.length() > 0)
              {
                  //Erase the quotation marks
                  line.erase(0,1);
                  line.erase(line.length()-1, 1);

                  unsigned int pos;

                  if((pos = line.find(":")) != string::npos) //If there is a : delimiting the string, continue
                  {
                      if(pos+2 < line.length()) //If the string is not rubbish data, continue
                      {
                          string subline1 = line.substr(0, pos-1); //The text before the :
                          string subline2 = line.substr(pos+2); //The text after the :
                          //If the line denotes the Monitor's serial number
                          if(subline1 == "Serial Number")
                          {
                              Monitor *monitor;
                              if((monitor = mh.getMonitorById(subline2))==NULL) //If this monitor does not exist, create it
                              {
                                  monitor = new Monitor(subline2);
                                  mh.addMonitor(monitor);
                                  cout << "Monitor added, Serial Number: " << subline2 << endl;
                              }
                          }
                          else if(mh.currentMonitor != NULL) //Begin ripping data from the file
                          {
                              //mh.currentMonitor->values.insert(pair<string,string>(subline1, subline2));
                              DataContainer *dc;
                              if((dc = mh.currentMonitor->getDataContainer(subline1)) == NULL) //If this DataContainer does not exist, create it
                              {
                                  dc = new DataContainer(subline1);
                                  mh.currentMonitor->dataContainers.push_back(dc);

                              }
                              dc->data.push_back(atof(subline2.c_str())); //Add the data to the appropriate DataContainer
                          }
                      }
                  }
                  //cout << line << endl;
              }
            }
            file.close();
        }
        else
        {
            cout << "Unable to open, aborting program!" << endl;
            return 0;
        }
      }
      closedir (dir);
      cout << "Listing monitors" << endl;
        for(int i = 0; i < mh.monitors.size(); i += 1)
        {
            cout << endl << "        ---Monitor " << mh.monitors[i]->id << "---" <<endl << endl;
            //Sort through all the data found in the files
            mh.monitors[i]->finalize();
            cout << "Median Tran Peak is" << mh.monitors[i]->values["Median TranPeak"] << endl;
            cout << "Average Tran Peak is " << mh.monitors[i]->values["Average TranPeak"] << endl;
            cout << "Lowest Tran Peak is " << mh.monitors[i]->values["Lowest TranPeak"] << endl;
            cout << "Highest Tran Peak is " << mh.monitors[i]->values["Highest TranPeak"] << endl;

            cout << "Median Vert Peak is" << mh.monitors[i]->values["Median VertPeak"] << endl;
            cout << "Average Vert Peak is " << mh.monitors[i]->values["Average VertPeak"] << endl;
            cout << "Lowest Vert Peak is " << mh.monitors[i]->values["Lowest VertPeak"] << endl;
            cout << "Highest Vert Peak is " << mh.monitors[i]->values["Highest VertPeak"] << endl;

            cout << "Median Long Peak is" << mh.monitors[i]->values["Median LongPeak"] << endl;
            cout << "Average Long Peak is " << mh.monitors[i]->values["Average LongPeak"] << endl;
            cout << "Lowest Long Peak is " << mh.monitors[i]->values["Lowest LongPeak"] << endl;
            cout << "Highest Long Peak is " << mh.monitors[i]->values["Highest LongPeak"] << endl;


        }
    } else {
      /* could not open directory */
      perror ("");
      return 1;
    }
    return 0;
}
